import prompt from '@ohos.prompt'
import {collision} from '../../common/js/collision';

// 子弹
/*
全局变量  getApp().data
myTankDir   我的坦克方向 0上 1 下 2 左 3 右
myTankPos  //我的坦克位置
MAP1         等级1地图
home         // 家的坐标
autoTankArray    //敌方坦克集合
drawMap      //画笔
autoTankNum   //敌方坦克数量
isHomeState //家的状态
mapSource //地图上所有的物体
*/
/**************坦克图片资源*****************/
var BULLET_RESOURCE_IMAGE = new Image();
BULLET_RESOURCE_IMAGE.src = "common/images/bullet.png";


export default class Bullet {
  constructor(owner, dir, pos) {
    this.drawMap = getApp().data.drawMap;
    this.count = false;
    this.timer = null; //设置一个定时器
    this.dir = dir; //子弹方向
    this.owner = owner; // 子弹拥有者 5玩家  4 AI
    switch (this.owner) {
      case 4: //autoTank
        this.type = 7;
        break;
      case 5: //myTank
        this.type = 6;
        break;
    }
    this.bulletPos0 = [0, 0]; //  pos 是发出这个子弹坦克的位置
    this.bulletPos = [0, 0];
    this.bulletPos0[0] = pos[0];
    this.bulletPos0[1] = pos[1];
    this.bulletPos[0] = pos[0];
    this.bulletPos[1] = pos[1];
  }

  run() {
    console.log("发射成功");
    this.timer = setInterval(() => {
      this.move();
    }, 500)
  }

  move() {
    //console.log(this.bulletPos);
    //console.log(this.bulletPos0);
    //删除
    if (this.count) {
      this.destroy(); // 初始的时候 防止子弹发出时把坦克删除了
    } else {
      this.count = true;
    }
    switch (this.dir) {
      case 0: // 上
        this.bulletPos[1] -= 1;
        break;
      case 1: //下
        this.bulletPos[1] += 1;
        break;
      case 2: //左
        this.bulletPos[0] -= 1;
        break;
      case 3: //右
        this.bulletPos[0] += 1;
        break;
    }
    //console.log("家的位置");
    // console.log(getApp().data.home[0][1]);
    switch (collision(this.bulletPos)) {
      case 0: // 无障碍
        this.drawMap.drawImage(BULLET_RESOURCE_IMAGE, this.bulletPos[0] * 50, this.bulletPos[1] * 50, 50, 50);
        getApp().data.mapSource[this.bulletPos[0]][this.bulletPos[1]] = this.type;
        console.log("子弹在移动");
        break;
      case 1: // softWall
        clearInterval(this.timer);
        this.destroy(); //删除该处的softWall
        console.log("子弹击毁墙");
        break;
      case 2: //  hardWall
        clearInterval(this.timer);
        console.log("子弹撞墙了");
        break;
      case 3: //home
        clearInterval(this.timer);
        this.destroy(); //删除该处的home
        getApp().data.isHomeState = false; //一旦家被子弹击毁，改变家的状态
        console.log("子弹击毁家");
        break;
      case 4: // autoTank
        this.collisionAutoTank();
        console.log("子弹击毁敌方坦克");
        break;
      case 5: // myTank
        this.collisionMyTank();
        console.log("子弹击毁我的坦克");
        break;
      case 6: //myBullet
        this.collisionMyBullet();
        console.log("子弹撞我的子弹");
        break;
      case 7: //bullet
        this.collisionBullet();
        console.log("子弹撞敌方子弹");
        break;
      case 8: //border
        clearInterval(this.timer); //子弹不再移动
        console.log("子弹撞边界了");
        break;
    }
  }

  //碰敌方坦克处理方法
  collisionAutoTank() {
    console.log("开始判断");
    console.log(this.owner);
    if (this.owner == 5) {
      clearInterval(this.timer); // 子弹不再移动
      getApp().data.autoTankArray.forEach(item => {
        if (item.pos[0] == this.bulletPos[0] && item.pos[1] == this.bulletPos[1]) {
          item.clear();
          item.isDestroyed = true;
          getApp().data.autoTankNum--;
        }
      })
      this.destroy();  //删除该处的autoTank
    } //4不显示 则继续移动
  }

  //碰我的坦克处理方法
  collisionMyTank() {
    if (this.owner == 4) {
      clearInterval(this.timer); //子弹不再移动
      getApp().data.isHomeState = false; //我的坦克被击毁就相当于家被摧毁
      this.destroy();

    } // 5不显示 则继续移动
  }

  //碰敌方子弹处理方法
  collisionBullet() {
    switch (this.type) {
      case 6:
        clearInterval(this.timer); //子弹不再移动
        this.destroy();  //
        break;
      case 7:
        this.drawMap.drawImage(BULLET_RESOURCE_IMAGE, this.bulletPos[0] * 50, this.bulletPos[1] * 50, 50, 50);
        getApp().data.mapSource[this.bulletPos[0]][this.bulletPos[1]] = this.type;
    }
  }

  //碰我的子弹处理方法
  collisionMyBullet() {
    switch (this.type) {
      case 7:
        clearInterval(this.timer); //子弹不再移动
        this.destroy();
        break;
      case 6:
        this.drawMap.drawImage(BULLET_RESOURCE_IMAGE, this.bulletPos[0] * 50, this.bulletPos[1] * 50, 50, 50);
        getApp().data.mapSource[this.bulletPos[0]][this.bulletPos[1]] = this.type;
    }
  }

  //子弹销毁
  destroy() {
    this.drawMap.clearRect(this.bulletPos[0] * 50, this.bulletPos[1] * 50, 50, 50);
    getApp().data.mapSource[this.bulletPos[0]][this.bulletPos[1]] = 0; //此处物体消失
  }
}